package com.andnux.android.fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.andnux.android.MainApp;
import com.andnux.android.activity.SearchActivity;
import com.andnux.android.activity.WorksDetailsActivity;
import com.andnux.android.adapter.OnItemClickListener;
import com.andnux.android.adapter.WorksAdapter;
import com.andnux.android.data.PageData;
import com.andnux.android.data.SystemUsers;
import com.andnux.android.data.Works;
import com.andnux.android.databinding.FragmentHomeBinding;
import com.andnux.android.databinding.ItemWorksBinding;
import com.andnux.android.network.DesignCloudApi;
import com.andnux.android.network.NotNullConsumer;
import com.andnux.android.network.Result;
import com.andnux.android.network.RetrofitClient;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class HomeFragment extends BaseFragment<FragmentHomeBinding> {

    private int current = 0;
    private static final int size = 10;
    private WorksAdapter adapter;
    private final List<Works> list = new ArrayList<>();


    @Override
    public void onViewCreated(@NonNull @NotNull View view, @Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        binding.search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(requireActivity(), SearchActivity.class));
            }
        });
        adapter = new WorksAdapter();
        adapter.setOnItemClickListener(new OnItemClickListener<ItemWorksBinding>() {
            @Override
            public void onItemClick(ItemWorksBinding view, int position, long id) {
                Intent intent = new Intent(requireContext(), WorksDetailsActivity.class);
                intent.putExtra("works", list.get(position));
                startActivity(intent);
            }
        });
        binding.content.setAdapter(adapter);
        binding.smart.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                current++;
                loadData();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                current = 0;
                loadData();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        loadData();
    }

    private void loadData() {
        DesignCloudApi api = RetrofitClient.getInstance().getServerApi();
        SystemUsers users = MainApp.getApp().getSystemUsers();
        String lx = "";
        if (users != null) {
            String role = users.getRole();
            if ("1".contentEquals(role)) {
                lx = "0";
            } else {
                lx = "1";
            }
        }
        addDisposable(api.worksPage(current, size, lx, "", "", 1)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new NotNullConsumer<Result<PageData<List<Works>>>>() {
                    @Override
                    public void onSuccess(Result<PageData<List<Works>>> result) {
                        if (current == 0) {
                            list.clear();
                        }
                        List<Works> works = result.getData().getList();
                        list.addAll(works);
                        adapter.setNewData(list);
                        if (works.size() < size) {
                            binding.smart.finishLoadMoreWithNoMoreData();
                        } else {
                            binding.smart.finishLoadMore();
                        }
                        binding.smart.finishRefresh();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        binding.smart.finishLoadMoreWithNoMoreData();
                        binding.smart.finishRefresh();
                        if (throwable != null) {
                            Toast.makeText(requireActivity(), throwable.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }));

    }
}